package ca.camosun.chatbox.network;

import java.io.IOException;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

class ConnectThread extends Thread {
    private final BluetoothSocket socket;
    private final BluetoothDevice mmDevice;
    
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 
    public ConnectThread(BluetoothDevice device) {

        BluetoothSocket tmp = null;
        mmDevice = device;
 
        try {
        	//Note: this UUID must be the same in the AcceptThread and the ConnectThread
            tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("b7a74330-7bdc-11e2-b92a-0800200c9a66"));
        } catch (IOException e) { }
        socket = tmp;
    }
 
    public void run() {
 
        try {
            socket.connect();
        } catch (IOException connectException) {
            try {
                socket.close();
            } catch (IOException closeException) { }
            return;
        }
 
        ConnectedThread connection = new ConnectedThread(socket);
    	connection.start();
    }
 
    public void cancel() {
        try {
            socket.close();
        } catch (IOException e) { }
    }
}
